package com.service.luo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.luo.CusDao;
import com.dao.luo.CusDaoM;
import com.dao.luo.CuswordDao;
import com.dao.luo.DrawtypeDao;
import com.entity.Customer;
import com.entity.Cusword;
import com.entity.Drawtype;
import com.entity.Linkman;
import com.entity.Word;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.ServiceException;
import com.dao.luo.LinkmanDao;
import com.dao.luo.WordDao;;

@Service
@Transactional(rollbackFor=Exception.class)
public class CusService {
	@Autowired
	private CusDao cusDao;
	@Autowired
	private LinkmanDao linkmanDao;
	@Autowired
	private CuswordDao cuswordDao;
	@Autowired
	private WordDao wordDao;
	@Autowired
	private DrawtypeDao drawtypeDao;
	@Autowired
	private CusDaoM cusDaoM;
	
	public void insertOne(Customer cus) {
		try {
<<<<<<< HEAD
			List<Linkman> linkmans = cus.getLinkmans(); 
=======
			
			/**
			 * 刷新联系人列表
			 */
			List<Linkman> linkmans = cus.getLinkmans();
			List<Linkman> links = new ArrayList<Linkman>();
>>>>>>> refs/heads/ljl
			if(linkmans!=null) {
				for(Linkman linkman:linkmans) {
					linkmanDao.save(linkman);
					linkman.setCustomers(new ArrayList<Customer>());
					linkman.getCustomers().add(cus);
				}
<<<<<<< HEAD
			}
=======
			}
			Cusdraw cusdraw = new Cusdraw();
			cusdrawDao.save(cusdraw);
			List<Drawtype> drawtypes = (List<Drawtype>) drawtypeDao.findAll();
			List<Drawword> drawwords = new ArrayList<Drawword>();
			for(Drawtype drawtype : drawtypes) {
				List<Word> words = drawtype.getWords();{
					for(Word word:words) {
						word = wordDao.findOne(word.getWordno());
						Drawword drawword = new Drawword();
						drawword.setWord(word);
						drawword.setCusdraw(cusdraw);
						drawwordDao.save(drawword);
						drawwords.add(drawword);
					}
				}
			}
			cusdraw.setDrawwords(drawwords);
			cusdrawDao.save(cusdraw);
			cus.setCusdraw(cusdraw);
			cus.setLinkmans(links);
>>>>>>> refs/heads/ljl
			cusDao.save(cus);
			List<Word> words = (List<Word>) wordDao.findAll();
			for(Word word:words) {
				word = wordDao.findOne(word.getWordno());
				Cusword cusword = new Cusword();
				cusword.setWord(word);
				cuswordDao.save(cusword);
				cusword.setCustomer(cus);
			}
		}catch(Exception e){
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 新建一个用户
	 * @param copname
	 * @param cname
	 * @param csource
	 * @param ctel
	 * @param cphone
	 * @param cremark
	 */
	public void createOne(String copname,String cname,String csource,String ctel,String cphone,String cremark) {
		Customer cus = new Customer();
		try {
			cus.setCname(copname);
			cus.setIsDelete(false);
			cusDao.save(cus);
			List<Word> words = (List<Word>) wordDao.findByOrderByWordnoAsc();
			for(Word word:words) {
				word = wordDao.findOne(word.getWordno());
				Cusword cusword = new Cusword();
				cusword.setWord(word);
				if(word.getWname()!=null) {
					if(word.getWname().equals("客户名称")) {
						cusword.setCwval(copname);
					}
					if(word.getWname().equals("简称")) {
						cusword.setCwval(copname);
					}
					if(word.getWname().equals("来源")) {
						cusword.setCwval(csource);
					}
					if(word.getWname().equals("移动电话")) {
						cusword.setCwval(ctel);
					}
					if(word.getWname().equals("备注")) {
						cusword.setCwval(cremark);
					}
				}
				cuswordDao.save(cusword);
				cusword.setCustomer(cus);
			}

			Linkman linkman = new Linkman();
			linkman.setLname(ctel);
			linkman.setLtel(ctel);
			linkman.setLphone(cphone);
			linkmanDao.save(linkman);
			linkman.setCustomers(new ArrayList<Customer>());
			linkman.getCustomers().add(cus);
		}catch(Exception e){
			throw new ServiceException(e);
		}
	}
	
	public void updateCustomer(Customer cus) {
		List<Cusword> cuswords=cus.getCuswords();
		for(Cusword cusword:cuswords) {
			System.out.println(cusword);
			cusword.setCustomer(cus);
			cuswordDao.save(cusword);
		}
		cusDao.save(cus);
	}
	
	public Map selectCus(Customer cus,Integer current,Integer size){
		List<Customer> cuses = cusDaoM.selectCus(cus);
		List<Customer> list = new ArrayList<Customer>();
		if(cuses.size()>=current*size) {
			list = cuses.subList((current-1)*size,current*size);
		}else {
			list = cuses.subList((current-1)*size,cuses.size());
		}
		Map map = new HashMap();
		map.put("total",cuses.size());
		map.put("cuses",list);
		return map;
	}
	
	public Customer selectCusByCusno(Long cusno) {
		return cusDaoM.selectCusByCusno(cusno);
	}
	
	public void deleteCus(Long cusno) {
		Customer cus = cusDao.findOne(cusno);
		cus.setIsDelete(true);
		cusDao.save(cus);
	}
	
	public Customer findOneCus(Long cusno) {
		return cusDao.findOne(cusno);
	}
	
//	public List<Customer> findAllCus(){
//		return (List<Customer>) cusDao.findAll();
//	}
	
	public List<Linkman> findOneCusLink(Long cusno) {
		List<Linkman> list=cusDao.findOne(cusno).getLinkmans();
		list.size();
		return list;
	}
}
